package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientFollowUp;
import cn.wolfcode.crm.domain.GuaranteeBill;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.ClientFollowUpMapper;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.GuaranteeBillMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClientFollowUpService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 21:45
 **/
@Service
public class ClientFollowUpServiceImpl implements IClientFollowUpService {
    @Autowired
    private ClientFollowUpMapper mapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private GuaranteeBillMapper guaranteeBillMapper;
    @Autowired
    private AccidentMapper accidentMapper;

    public void save() {
        ClientFollowUp entity = new ClientFollowUp();
        //获取正式客户的所有客户对象
        List<Client>  clients = clientMapper.seletByState(0L);
        for (Client client : clients) {
            //给正式客户报表的客户设值
            entity.setClient(client);
            //获取正式客户的所有保单信息汇总
            GuaranteeBill gb = guaranteeBillMapper.selectbyClientId( client.getId());
            entity.setAuditDate(gb.getAuditDate());
            entity.setNumber(gb.getNumber());
            entity.setTotalAmont(gb.getTotalPrice());
            entity.setVdate(gb.getVdate());
            //获取理正式客户的理赔状态
            Integer state = accidentMapper.selectByState(client.getId());

            entity.setState(state);
            mapper.insert(entity);

        }
    }

    public void update(ClientFollowUp entity) {

        mapper.updateByPrimaryKey(entity);
    }

    public void delete(long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public ClientFollowUp get(long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<ClientFollowUp> list() {
        return mapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<ClientFollowUp> list = mapper.queryForList(qo);
        return new PageResult(total, list);
    }
}
